package com.xxxx.controller;

import com.xxxx.model.User;
import com.xxxx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;
    @RequestMapping("/tologin")
    private String tologin(Model model){
        return "login";
    }
    @RequestMapping("/toregist")
    private String toregist(Model model){
        return "regist";
    }
    @RequestMapping("/addUser")
    private String regist(User user,Model model){
        if (user.getUserID().equals("")){
            model.addAttribute("register","请输入账号信息");
            return "regist";
        }
        if (user.getPassword().equals("")){
            model.addAttribute("register","请输入密码");
            return "regist";
        }
        if (user.getUname().equals("")){
            model.addAttribute("register","请输入姓名");
            return "regist";
        }
        int i = userService.addUser(user);
        if (i !=0){
            model.addAttribute("register","注册成功");
        }
        return "regist";
    }
    @ResponseBody
    @RequestMapping("/a3")
    public String ajax(String userID){
        String idMsg="";
        List<String> strings = userService.allID();
        if (userID.equals("")) {
            idMsg="请先填写账号";
        }else {
            idMsg="OK";
            for (String string : strings) {
                if (string.equals(userID)){
                    idMsg="该账号已被注册，请登录";
                }
            }
        }
        return idMsg;
    }
    /*@ResponseBody
    @RequestMapping("/a4")
    public String ajax1(String userID){
        String idMsg="";
        List<String> strings = userService.allID();
        *//*for (String string : strings) {
            if (!string.equals(userID)){
                idMsg="OK";
            }
        }
        return idMsg;*//*
    }*/
    @RequestMapping("/login")
    public String login(User user, Model model, HttpSession session,String identity){
        if (user.getUserID().equals("")){
            model.addAttribute("msg","请输入账号信息");
            return "login";
        }
        if (user.getPassword().equals("")){
            model.addAttribute("msg","请输入密码");
            return "login";
        }
        if (identity.equals("用户")){
            User user1 = userService.login(user.getUserID());
            if (user1==null){
                model.addAttribute("msg","不允许为空");
                return "login";
            }
            if (user1.getPassword().equals(user.getPassword())){
                session.setAttribute("userID",user.getUserID());
                return "User";
            }
            else model.addAttribute("msg","密码错误");
        }
        if (identity.equals("管理员")){
            User user2 = userService.login(user.getUserID());
            if (user2==null){
                model.addAttribute("msg","不允许为空");
                return "login";
            }
            if (user2.getPassword().equals(user.getPassword())){
                session.setAttribute("adminID",user.getUserID());
                return "redirect:/queryall";
            }
            else model.addAttribute("msg","密码错误");
        }
        return "login";
    }

}
